package app.modules.cloudpivot.function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.authine.cloudpivot.engine.api.facade.RoleFacade;
import com.authine.cloudpivot.engine.api.facade.UserFacade;
import com.authine.cloudpivot.engine.workflow.spi.context.WorkflowActivityContext;
import com.authine.cloudpivot.engine.workflow.spi.function.ParticipantFunction;
import com.authine.cloudpivot.foundation.util.api.SpringBeanUtils;

/**
 * 顺序审批 参与者函数
 *
 * @author LiuGangQiang Create in 2025/04/01
 */
public class SequenceApprovalParticipantFunction extends ParticipantFunction {
    private static final long serialVersionUID = -173931992492578947L;
    private static final Logger LOG = LoggerFactory.getLogger(SequenceApprovalParticipantFunction.class);

    /**
     * 找人函数
     *
     * @author LiuGangQiang Create in 2025/04/01
     * @param userIdOrOrgId   用户ID或者部门ID
     * @param roleCode        角色编码
     * @param activityContext 上下文
     * @return {@link List}
     */
    public List<String> findParticipants(String userIdOrOrgId, String roleId, WorkflowActivityContext activityContext) {
        UserFacade userFacade = SpringBeanUtils.applicationContext.getBean(UserFacade.class);
        RoleFacade roleFacade = SpringBeanUtils.applicationContext.getBean(RoleFacade.class);
        List<String> userIds = new ArrayList<>();
        userIds.addAll(userFacade.listIdByUserIdsAndRoleCode(Arrays.asList(userIdOrOrgId), roleFacade.get(roleId).getCode(), true));
        userIds.addAll(userFacade.listUserIdByRoleCodeWithScope(Arrays.asList(userIdOrOrgId), roleFacade.get(roleId).getCode(), true));
        LOG.info("===>>顺序审批函数 入参userIdOrOrgId：{} roleCode：{}", userIdOrOrgId, roleId);
        /* TODO 这里拿到的userIds就是参与者，可以自行进行排序 */
        return userIds;
    }

    @Override
    public String getName() {
        return "SequenceApproval";
    }

    @Override
    public String getFullName() {
        return "SequenceApproval(,)";
    }

    @Override
    public String getDisplayName() {
        return "顺序审批函数";
    }

    @Override
    public String getDescription() {
        return "SequenceApproval(userIdOrOrgId,roleId)顺序审批函数，userIdOrOrgId参数可以是人员单选或者部门单选控件，roleId是角色直接选择即可。找人逻辑是寻找管辖范围包含用户或者组织的对应角色的用户然后根据自由逻辑进行排序的顺序审批";
    }
}